package com.my.zuji.controller;

import javax.validation.Valid;

import jxl.Sheet;
import jxl.Workbook;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.my.zuji.controller.form.LoginForm;
import com.my.zuji.controller.form.RegisteForm;
import com.my.zuji.domain.bookkeeping.User;
import com.my.zuji.service.PublicService;

@Controller
@SessionAttributes("currUser")
public class UserController {
	@Autowired
	private PublicService publicService;

	@RequestMapping("/check_userName")
	@ResponseBody
	public Boolean check_userName(String userName) {
		return publicService.checkUserName(userName);
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login(Model model) {
		return new ModelAndView("login").addObject(new LoginForm());
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView doLogin(@Valid LoginForm loginForm,
			BindingResult result, Model model) {
		if(result.hasErrors()){
			return new ModelAndView("login");
		}
		User user = publicService.login(loginForm);
		if (user == null) {
			ModelAndView mv = new ModelAndView("login");
			mv.addObject("loginError","用户名或密码错误");
			return mv;
		}
		model.addAttribute("currUser", user);
		return new ModelAndView("redirect:/index");
	}

	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public ModelAndView register(Model model) {
		return new ModelAndView("register").addObject(new RegisteForm());
	}
	
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public ModelAndView doRegister(@Valid RegisteForm registeForm,
			BindingResult result,Model model) {
		if(!registeForm.getPassword().equals(registeForm.getRePassword())){
			result.rejectValue("password","Pattern","* 两次密码输入不一致！");
		}
		if (result.hasErrors()) {
			return new ModelAndView("register");
		}
		publicService.register(registeForm);
		ModelAndView mv = new ModelAndView("redirect:/login");
		return mv;
	}
	
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public RedirectView logout(SessionStatus sessionStatus) {
		sessionStatus.setComplete();
		return new RedirectView("login");
	}

	@RequestMapping(value = {"/","/index"}, method = RequestMethod.GET)
	public ModelAndView index(Model model) {
		return new ModelAndView("index");
	}
	
	@RequestMapping(value ="/our", method = RequestMethod.GET)
	public ModelAndView our(Model model) {
		return new ModelAndView("our");
	}
	
	@RequestMapping(value = "/excel", method = RequestMethod.GET)
	public ModelAndView excel(Model model) {
		return new ModelAndView("excel");
	}
	
	@RequestMapping(value = "/excel", method = RequestMethod.POST)
	public ModelAndView excel(@RequestParam("file") MultipartFile file) {
		try {
			Workbook workbook = Workbook.getWorkbook(file.getInputStream());
			Sheet sheet = workbook.getSheet(0);
			int rows = sheet.getRows();
			int cols=sheet.getColumns();
			for (int r = 1; r < rows; r++) {
				for(int c = 0; c < cols; c++){
					String contents = sheet.getCell(c, r).getContents();
					if(StringUtils.isNotBlank(contents)){
						if(cols-2==c){
							System.out.println(contents+"-*-");
						}else{
							System.out.print(contents+"-*-");
						}
					}else{break;}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ModelAndView("excel");
	}
}
